<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <script src="../../js/load.js"></script>
</head>

<body>
<div id="container">
    <div id="formcontainer">
        <form class="layui-form layui-form-pane" id="editform" lay-filter="editform">
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <div class="layui-form-item">
                        <label class="layui-form-label required" style="width: 140px;">key</label>
                        <div class="layui-input-block" style="margin-left: 140px;">
                            <input type="text" name="key" autocomplete="off" class="layui-input required" disabled>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <div class="layui-form-item">
                        <label class="layui-form-label required"  style="width: 140px;">过期时间（s）</label>
                        <div class="layui-input-block"  style="margin-left: 140px;">
                            <input type="text" name="keyExpire" id="keyExpire" autocomplete="off" class="layui-input required number">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <div class="layui-form-item">
                        <label class="layui-form-label required" style="width: 140px;">数据类型</label>
                        <div class="layui-input-block"  style="margin-left: 140px;">
                            <select name="keyType" lay-verify="required" lay-filter="keyType" disabled>
                                <option value="string">string</option>
                                <option value="list">list</option>
                                <option value="set">set</option>
                                <option value="zset">zset</option>
                                <option value="hash">hash</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">value</label>
                        <div class="layui-input-block">
                            <table class="layui-table" lay-data="{id: 'datagrid',height:'full'}" lay-filter="datagrid">
                                <thead>
                                <tr>
                                    <th lay-data="{field:'key'}">key</th>
                                    <th lay-data="{field:'value'}">value</th>
                                    <th lay-data="{fixed: 'right', width:175, align:'center', toolbar: '#op'}">操作</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div id="opcontainer">
        <button class="layui-btn" onclick="UpdateExpire()">更新过期时间</button>
        <button class="layui-btn" onclick="AddRow()">添加行</button>
        <button class="layui-btn layui-btn-primary" onclick="HandleClose()">关闭</button>
    </div>
</div>
</body>
<script type="text/html" id="op">
    <button type="button" class="layui-btn layui-btn-sm" lay-event="EditRow"><i
            class="layui-icon layui-icon-edit layui-font-12"></i>编辑
    </button>
    <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="DeleteRow"><i
            class="layui-icon layui-icon-delete layui-font-12"></i>删除
    </button>
</script>
</html>
<script>
    var FormDetail = null;

    $(function () {
        LoadDetail();
    })

    function LoadDetail() {
        var param = {
            key: GetUrlParam("key"),
            dbIndex:GetUrlParam("dbIndex")
        }
        SetFormV2("editform", "api/redis/getValueDetail", param, function (data) {
            FormDetail = data;
            BindStaticTable('datagrid',data.value);
        })
    }
    
    function UpdateExpire() {
        var param = {
            key: GetUrlParam("key"),
            dbIndex:GetUrlParam("dbIndex"),
            keyExpire:$("#keyExpire").val()
        }
        HttpPost(JSON.stringify(param), 'api/redis/updateExpire', function (result) {
            if (result.code == 0) {
                OpenSuccess('更新成功', function (params) {
                    LoadDetail();
                })
            } else {
                OpenFail(result.msg);
            }
        })
    }

    function AddRow() {
        var param = {
            dbIndex:GetUrlParam("dbIndex"),
            key: GetUrlParam("key"),
        }
        OpenDialog('添加行', 'pages/redis/redis_addrow.html?keyType=hash' , 600, 500, function () {
            LoadDetail();
        },param)
    }

    function EditRow(RowData) {
        var param = {};
        param.dbIndex = GetUrlParam("dbIndex");
        param.key = GetUrlParam("key");
        param.hashKey = RowData.key;
        param.value = RowData.value;
        OpenDialog('添加行', 'pages/redis/redis_editrow.html?keyType=hash' , 600, 500, function () {
            LoadDetail();
        },param)
    }

    function DeleteRow(RowData) {
        OpenConfirm('确认删除？', function () {
            var param = {
                dbIndex:GetUrlParam("dbIndex"),
                key: GetUrlParam("key"),
                hashKey:RowData.key
            }
            HttpPost(JSON.stringify(param), 'api/redis/deleteRow', function (result) {
                if (result.code == 0) {
                    OpenSuccess('删除成功', function (params) {
                        LoadDetail();
                    })
                } else {
                    OpenFail(result.msg);
                }
            })
        })
    }

    function HandleClose() {
        CloseDialog();
    }
</script>